package com.zjl.spring;

import com.zjl.spring.config.User;
import org.springframework.boot.ExitCodeGenerator;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.AbstractEnvironment;
import org.springframework.util.StringUtils;

@SpringBootApplication
public class Application {
    public static final String ACTIVE_PROFILES_PROPERTY_NAME = "spring.profiles.active";

    //	@Bean
//	public ExitCodeGenerator exitCodeGenerator() {
//		return () -> 42;
//	}
//
//	public static void main(String[] args) {
//		int exit = SpringApplication.exit(SpringApplication.run(Application.class, args));
//		System.exit(exit);
//
//	}
    public static void main(String[] args) {
        String env = System.getProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME);

        if(StringUtils.isEmpty(env)) {
            for(String arg : args) {
                if(arg.contains(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME)) {
                    System.setProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, arg.split("=")[1]);
                }
            }
        }

        ConfigurableApplicationContext run = SpringApplication.run(Application.class, args);
        User bean = run.getBean(User.class);
        System.out.println(bean.getName());
        System.out.println(bean.getMyprop());

    }


}
